home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.lang.c
- Path: txnews.amd.com!news
- From: Bret Patterson <faustus>
- Subject: Re: Performance: C vs. C++
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <DLr46y.7rH@txnews.amd.com>
- Sender: news@txnews.amd.com
- Nntp-Posting-Host: fuggles
- Content-Transfer-Encoding: 7bit
- Organization: Advanced Micro Devices, Inc., Austin, Texas, USA
- References: <30F6BAAC.12B5@iastate.edu> <4da9pn$a45@news.bridge.net> <4dnpl2$c8g@classic.iinet.com.au> <3105E9DC.1BE3@enermet.fi>
- Mime-Version: 1.0
- Date: Thu, 25 Jan 1996 19:16:09 GMT
- X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.05 9000/715)
- X-Url: news:3105E9DC.1BE3@enermet.fi
-
- Harri Halttunen <Harri.Halttunen@enermet.fi> wrote:
- >> Not quite. C is quite different from C++ from the performance point of
- >> view. The difference is not only in virtual functions and exceptions
- >> but in class construction, destruction, etc... and most significantly in
- >> Runtime Type Checking. However, because machines are getting faster by
- >> the day, this sort of "inefficiency" is hardly noticable.
- >
- >All that is true (at least in the some point of view), but we have to remember
- >that usually in real applications this kind of comparison doesn't matter. This
- >is because fetching something from a database or file takes many times longer
- >than, for example, a virtual function call or to handle an excepion. Or even
- >check a type of an object at runtime.
-
- Except what most people use virtual functions for is to avoid large switch
- statements and make code more easily changed, more readable etc.
-
- I would say a*WELL written* C++ program is faster than a *Well written* C program that does the
- exact same thing. Notice *Well written* because most c++ programmers are in their infancy
- and blame their bad coding on the language.
-
- Here is an example to illustrate why virtual functions do not cause extra overhead.
-
- C program:
-
- switch (object->type)
- {
- case 1 : DragonAttack();
- case 2 : OrcAttack();
- ...
- };
-
- and a C++:
- object->Attack(); // which internally g++ does a object->virtualTable[Function#]();
-
- Big deal. So c++ does a virtual table lookup, but in order for C to have this ability
- it has to use a switch statement or ifelse construct. I would say with good compiler
- optimization C++ is going to be faster and more readable.
-
- Of course C++ uses 4bytes for the pointer while in C you would use a char to represent
- type. But if the extra 3 bytes per object is a major problem then you wouldn't want that
- extra byte in C either. You would use another method.
-
- >I use C++ mainly, because it supports code and idea reuse better than other
- >languages.
-
- The only thing of C++ that I know causes alot of overhead is exceptions. But other forms
- of error checking are available and exceptions can also be optimized/minimized.
-
- Bret
-
-